package gk.example.activity;

import gk.example.App;
import gk.example.BaseActivity;
import gk.example.bean.User;
import gk.example.util.ErrorCodeUtil;
import gk.example.util.T;
import gk.example.view.ProgressDialog;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

import com.example.gkdate.R;

public class PreviewActivity extends BaseActivity implements OnClickListener {
	private Button mBtnClip;
	private ImageView mImagePreview;
	private ProgressDialog mProgressDialog;
	private TextView mTvTitle;
	private String mPath;
	private ImageButton mBtnBack;

	@Override
	protected void initView() {
		setContentView(R.layout.activity_preview);
		mBtnClip = (Button) findViewById(R.id.btn_clip);
		mImagePreview = (ImageView) findViewById(R.id.image_preview);
		mTvTitle = (TextView) findViewById(R.id.tv_top_title);
		mBtnBack = (ImageButton) findViewById(R.id.btn_back);
		mProgressDialog = new ProgressDialog();
	}

	@Override
	protected void initEvent() {
		mTvTitle.setText("预览");
		mBtnClip.setOnClickListener(this);
		mBtnBack.setOnClickListener(this);
		mBtnClip.setVisibility(View.VISIBLE);
		byte[] bis = getIntent().getByteArrayExtra("bitmap");
		mPath = getIntent().getStringExtra("path");
		Bitmap bitmap = BitmapFactory.decodeByteArray(bis, 0, bis.length);
		mImagePreview.setImageBitmap(bitmap);
		File file = new File(mPath);
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file);
			fos.write(bis);
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		if (bitmap != null) {
		}
	}

	/**
	 * 
	 * 描述：上传头像
	 * 
	 * @param picdata
	 * @return void
	 */
	private void setPicToView() {
		mProgressDialog.setMsg("上传中");
		mProgressDialog.show(getSupportFragmentManager(), "uploading");
		final BmobFile file = new BmobFile(new File(mPath));
		file.uploadblock(this, new UploadFileListener() {

			@Override
			public void onSuccess() {
				setUserHead(file);
			}

			@Override
			public void onFailure(int arg0, String arg1) {
				ErrorCodeUtil.Errorshow(PreviewActivity.this, arg0, arg1);
				mProgressDialog.dismiss();
			}
		});

	}

	/**
	 * 
	 * 描述： 修改用户头像
	 * 
	 * @return void
	 */
	private void setUserHead(final BmobFile bmobFile) {
		User user = new User();
		user.setObjectId(App.BmobUser.getObjectId());
		user.setUserHead(bmobFile);
		user.update(this, new UpdateListener() {
			@Override
			public void onSuccess() {
				T.showShort(PreviewActivity.this, "修改成功");
				App.BmobUser.setUserHead(bmobFile);
				finish();
				mProgressDialog.dismiss();
				startActivity(new Intent(PreviewActivity.this,
						PersonalInfoActivity.class));
			}

			@Override
			public void onFailure(int arg0, String arg1) {
				ErrorCodeUtil.Errorshow(PreviewActivity.this, arg0, arg1);
				mProgressDialog.dismiss();
			}
		});

	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			startActivity(new Intent(this, PersonalInfoActivity.class));
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_clip:
			setPicToView();
			break;
		case R.id.btn_back:
			startActivity(new Intent(this, PersonalInfoActivity.class));
			finish();
			break;
		default:
			break;
		}

	}
}
